'use strict'

let templateConfig = require('../../config/templateConfig')
let attackHandle = require('./attackHandle')
let utils = require('../utils/utils')

exports.latticeAction = function(player) {
  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  let latticeType = latticeConfig.latticeType

  let adventureInfo = {
    secretType: latticeType,
    type: 0
  }

  //1岗哨2入口3草原类型4温泉类型5湿地采集地形6矿井采矿地形
  //7池塘钓鱼类型8秘境小屋9幻境类型10墓地11沼泽伤害地形类型12副本洞窟类型
  switch (latticeType) {
    case 3: //草地
      var random = Math.random() * 100
      var eventRate = latticeConfig.eventRate
      if (random < eventRate[0]) {
        var battleMsg = attackHandle.attackNormalMonster(player)
        adventureInfo.battleMsg = battleMsg
        adventureInfo.type = 1 //1草地遇到怪物
        break
      }
      random -= eventRate[0]
      if (random < eventRate[1]) { //触发事件
        var eventPool = latticeConfig.eventPool
        var events = templateConfig.other[eventPool].events
        var eventType = utils.getOneFromArray(events)
        player.basicInfo.secretMap[lattice]['eventType'] = eventType
        adventureInfo.type = eventType
        break
      }
      break
    case 11: //沼泽
      var hurt = latticeConfig.hurt[0] * player.basicInfo.lvl + latticeConfig.hurt[1]
      hurt =  utils.addRandomHalf(hurt)
      player.fightAttri.hp = player.fightAttri.hp - hurt
      if (player.fightAttri.hp < 0) {
        hurt += player.fightAttri.hp
        player.fightAttri.hp = 0
      }
      adventureInfo['hurt'] = hurt
      break
  }
  player.basicInfo.secretMap[lattice].eventType = adventureInfo.type
  player.basicInfo.state = 1
  return adventureInfo
}

exports.triggerEvent = function(player, eventType, eventPool) {
  let eventContent = eventPool[eventType]
  let getRand = Math.floor(Math.random() * 2)

  switch (eventType) {
    case 2: //随机获得物品 扣除少量生命
    case 3:
    case 7:
      if (player.basicInfo.stamina <= 2) {
        return {
          error: 4,
          eventType: eventType
        }
      } else {
        player.basicInfo.stamina -= 1
      }
      if (getRand) {
        var props = eventContent.getProps
        var propId = utils.getOneFromArray(props)
        var itemComponent = templateConfig.total[propId].getItemComponent()
        player.adventureBag.push(itemComponent)
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
          propId: propId
        }
      } else {
        var value = eventContent.value[0] * player.fightAttri.lvl
        value =  utils.addRandomHalf(value)
        player.fightAttri.hp -= value
        if (player.fightAttri.hp < 0) {
          value += player.fightAttri.hp
          player.fightAttri.hp = 0
        }
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
          hurt: value
        }
      }
      break
    case 4: //喝水 恢复少量生命 扣除少量生命
      if (player.basicInfo.stamina <= 2) {
        return {
          error: 4,
          eventType: eventType
        }
      } else {
        player.basicInfo.stamina -= 1
      }
      if (getRand) {
        var value = eventContent.value[0] * player.fightAttri.lvl
        value =  utils.addRandomHalf(value)
        player.fightAttri.hp += value
        if (player.fightAttri.hp > player.fightAttri.maxHp) {
          player.fightAttri.hp = player.fightAttri.maxHp
        }
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
          recoverHp: value
        }
      } else {
        var value = eventContent.value[1] * player.fightAttri.lvl
        value =  utils.addRandomHalf(value)
        player.fightAttri.hp -=  value
        if (player.fightAttri.hp < 0) {
          value += player.fightAttri.hp
          player.fightAttri.hp = 0
        }
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
          hurt: value
        }
      }
      break
    case 5: //打碎瓦罐 随机获得物品 什么都没有
      if (player.basicInfo.stamina <= 2) {
        return {
          error: 4,
          eventType: eventType
        }
      } else {
        player.basicInfo.stamina -= 1
      }
      if (getRand) {
        var props = eventContent.getProps
        var propId = utils.getOneFromArray(props)
        var itemComponent = templateConfig.total[propId].getItemComponent()
        player.adventureBag.push(itemComponent)
        return {
          error: 0,
          basicInfo: player.basicInfo,
          eventType: eventType,
          propId: propId
        }
      } else {
        return {
          error: 0,
          eventType: eventType,
        }
      }
      break
    case 6: //受到母兽的祝福随机获取buff 受到母兽攻击损失血量
      if (player.basicInfo.stamina <= 2) {
        return {
          error: 4,
          eventType: eventType
        }
      } else {
        player.basicInfo.stamina -= 1
      }
      if (getRand) {
        var value = eventContent.value[0]
        value =  utils.addRandomHalf(value)
        player.basicInfo.luckyBuff += value
        player.calcAttri()
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
          luckyBuff: value
        }
      } else {
        var value = eventContent.value[1] * player.fightAttri.lvl
        value =  utils.addRandomHalf(value)
        player.fightAttri.hp -= value
        if (player.fightAttri.hp < 0) {
          value += player.fightAttri.hp
          player.fightAttri.hp = 0
        }
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
          hurt: value
        }
      }
      break
    case 8: //救治伤者（花费治疗药剂x1） 随机获得物品 什么都没有
      var cureBarItem = player.cureBar
      if (!cureBarItem) {
        return {
          error: 6,
          eventType: eventType
        }
      }
      cureBarItem.stack -= 1
      if (cureBarItem.stack === 0) {
        player.cureBar = null
      }
      if (getRand) {
        var props = eventContent.getProps
        var propId = utils.getOneFromArray(props)
        var itemComponent = templateConfig.total[propId].getItemComponent()
        player.adventureBag.push(itemComponent)
        return {
          error: 0,
          basicInfo: player.basicInfo,
          eventType: eventType,
          propId: propId
        }
      } else {
        return {
          error: 0,
          eventType: eventType,
          basicInfo: player.basicInfo,
        }
      }
      break
    case 9: //休息一下（花费银币*lvl） 恢复体力
      var needMoney = eventContent.needMoney * player.basicInfo.lvl
      if (player.basicInfo.money < needMoney) {
        return {
          error: 3,
          eventType: eventType
        }
      }
      player.basicInfo.money -= needMoney
      var value = eventContent.value[0]
      player.basicInfo.stamina += value
      return {
        error: 0,
        eventType: eventType,
        stamina: value,
        basicInfo: player.basicInfo,
      }
      break
    case 10: //购买补给（花费银币*lvl） 获取治疗药剂
      var needMoney = eventContent.needMoney * player.basicInfo.lvl
      var value = eventContent.value[0]
      if (player.basicInfo.money < needMoney) {
        return {
          error: 3,
          eventType: eventType
        }
      }
      player.basicInfo.money -= needMoney
      var cureBarItem = player.cureBar
      if (!cureBarItem) {
        let itemComponent = templateConfig.total[240101].getItemComponent(1)
        player.cureBar = itemComponent
      } else {
        cureBarItem.stack += 1
      }
      return {
        error: 0,
        eventType: eventType,
        basicInfo: player.basicInfo,
      }
      break
    case 11: //参加赌博（花费1金币）
      var needGold = eventContent.needGold
      if (player.basicInfo.gold < needGold) {
        return {
          error: 5,
          eventType: eventType
        }
      }
      player.basicInfo.gold -= needGold
      var props = eventContent.getProps
      var propId = utils.getOneFromArray(props)
      var itemComponent = templateConfig.total[propId].getItemComponent()
      player.adventureBag.push(itemComponent)
      return {
        error: 0,
        basicInfo: player.basicInfo,
        eventType: eventType,
        propId: propId
      }
      break
    case 12: //祈祷（花费1金币） 获取幸运buff
      var needGold = eventContent.needGold
      var value = eventContent.value[0]
      if (player.basicInfo.gold < needGold) {
        return {
          error: 5,
          eventType: eventType
        }
      }
      player.basicInfo.gold -= needGold
      var value = eventContent.value[0]
      value =  utils.addRandomHalf(value)
      player.basicInfo.luckyBuff += value
      player.calcAttri()
      return {
        error: 0,
        eventType: eventType,
        basicInfo: player.basicInfo,
        luckyBuff: value
      }
      break
  }
}

//运送物资
exports.transportAction = function(player, lattice) {
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  let latticeType = latticeConfig.latticeType
  let adventureInfo = {
    secretType: latticeType,
    type: 0
  }
  switch (latticeType) {
    case 3: //草地
      if (Math.random() * 100 < latticeConfig.robberRate) {
        var fightMsg = attackHandle.attackRobber(player)
        adventureInfo.fightMsg = fightMsg
        adventureInfo.type = 1 //1草地遇到怪物
      } else {
        return 0
      }
      break;
    case 11: //沼泽
      var hurt = latticeConfig.hurt[0] * player.basicInfo.lvl + latticeConfig.hurt[1]
      hurt -= Math.floor(Math.random() * hurt - hurt / 2)
      player.fightAttri.hp = player.fightAttri.hp - hurt
      if (player.fightAttri.hp < 0) {
        player.fightAttri.hp = 0
        hurt = player.fightAttri.hp
      }
      adventureInfo['hurt'] = hurt
      break;
    default:
      return 0
  }
  return adventureInfo
}

//创建秘境地图
exports.createSecret = function(player, secretMapConfig) {
  player.basicInfo.secretMap = []
  player.basicInfo.secretMap[1] = {
    latticeId: secretMapConfig.latticeStart
  }

  let latticeId = 2
  let latticeMap = secretMapConfig.latticeMap
  for (let i in latticeMap) {
    let map = latticeMap[i]
    let index = Math.floor(Math.random() * map.length)
    let lattice = map[index]
    for (let j in lattice) {
      player.basicInfo.secretMap[latticeId] = {
        latticeId: lattice[j]
      }
      latticeId += 1
    }
  }
  player.basicInfo.secretMap[latticeId] = {
      latticeId: secretMapConfig.latticeEnd
  }
}

//体力为空
exports.returnCamp = function(player) {
  //返回营地
  player.basicInfo.luckyBuff = 0
  player.basicInfo.moneyBuff = 0
  player.basicInfo.maxHpBuff = 0
  player.calcAttri()
  player.fightAttri.hp = player.fightAttri.maxHp
  player.basicInfo.stamina = player.fightAttri.maxStamina
  player.basicInfo.lattice = 0
  player.basicInfo.secretMap = 0
  player.basicInfo.mapIndex = 1
  player.basicInfo.secretMapId = 0
  player.adventureBag = []
}
